package com.sgb.kjwl.view.widget.manage;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;

import androidx.annotation.ColorRes;

import com.sgb.kjwl.R;

public class StatusView extends androidx.appcompat.widget.AppCompatTextView {
    private Context mContext;
    private Paint mPaint;
    private Path path = new Path();
    private int mBgColor;
    public StatusView(Context context) {
        this(context, null);
    }

    public StatusView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatusView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.statusView);
        mBgColor = typedArray.getColor(R.styleable.statusView_bg_triangle, getResources().getColor(R.color.color_secondary_yellow));
        typedArray.recycle();
        this.mContext = context;
        init();
        setBackgroundResource(0);
    }


    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
    }

    public void setBgColor(@ColorRes int color){
        mBgColor = getResources().getColor(color);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        path.moveTo(getWidth() , 0);// 此点为多边形的起点
        path.lineTo(0, 0);
        path.lineTo(getWidth(), getHeight());
        path.close(); // 使这些点构成封闭的多边形
        mPaint.setColor(mBgColor);
        canvas.drawPath(path, mPaint);
        canvas.save();
        canvas.restore();
        invalidate();

        //倾斜度45,上下左右居中
        canvas.rotate(45, getWidth() / 2, getHeight() / 2);
        canvas.translate( 0, -getHeight() / 4);

        super.onDraw(canvas);
    }
}
